package xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class DomTest {
	public static void main(String[] args) throws Exception {
//		printXML();
		createXML();
	}

	private static void createXML() throws ParserConfigurationException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder b = factory.newDocumentBuilder();
		Document doc =b.newDocument();
		//创建节点
		Element book = doc.createElement("book");
		Element title = doc.createElement("title");
		Element authors = doc.createElement("authors");
		Element author1= doc.createElement("author");
		Element author2= doc.createElement("author");
		Text java = doc.createTextNode("java");
		Text name1 = doc.createTextNode("zs");
		Text name2 = doc.createTextNode("ls");
		//挂节点
		doc.appendChild(book);
		book.appendChild(title);
		book.appendChild(authors);
		title.appendChild(java);
		authors.appendChild(author1);
		authors.appendChild(author2);
		author1.appendChild(name1);
		author2.appendChild(name2);
		//设置属性
		book.setAttribute("isbn", "aslk");
		//将DOM树输出为XML文本
		OutputFormat f = new OutputFormat();
		f.setIndent(2);
		XMLSerializer serializer = new XMLSerializer(System.out,f);
		serializer.serialize(doc);
		
		File bok = new File("/home/tarena/Desktop/book.xml");
		FileOutputStream out = new FileOutputStream(bok);
		serializer = new XMLSerializer(out,f);
		serializer.serialize(doc);
	}

	private static void printXML() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder b = factory.newDocumentBuilder();
		Document doc = b.parse(new File(
				"/home/tarena/0hean/hean/src/xml/email.xml"));
		print(doc);
		
		//此方法有问题
		Element e = doc.getElementById("1");
		System.out.println(e);
//		System.out.println(e.getFirstChild().getNodeValue());
		
		NodeList tags = doc.getElementsByTagName("to-email");
		for (int i = 0; i < tags.getLength(); i++) {
			String email = tags.item(i).getFirstChild().getNodeValue();
			System.out.println(email);
		}
	}

	private static void print(Node	node) {
		short type = node.getNodeType();
		switch (type) {
		case Node.DOCUMENT_NODE:
			System.out.println("<?xml version='1.0' ?>");
			NodeList ch = node.getChildNodes();
			for (int i = 0; i < ch.getLength(); i++) {
				Node n = ch.item(i);
				print(n);
			}
			break;
		case Node.ELEMENT_NODE:
			System.out.print("<"+node.getNodeName());
			NamedNodeMap attr = node.getAttributes();
			for (int i = 0; i < attr.getLength(); i++) {
				Node att = attr.item(i);
				String name = att.getNodeName();
				String value = att.getNodeValue();
				System.out.print(" "+name+"=\""+value+"\"");
			}
			System.out.print(">");
			ch = node.getChildNodes();
			for (int i = 0; i < ch.getLength(); i++) {
				Node n = ch.item(i);
				print(n);
			}
			System.out.print("</"+node.getNodeName()+">");
			break;
		case Node.COMMENT_NODE:
			System.out.print("<--"+node.getNodeValue()+"-->");
			break;
		case Node.TEXT_NODE:
		case Node.CDATA_SECTION_NODE:
			System.out.print(node.getNodeValue());
			break;
		}
	}
	

}
